package com.huangpuweb.api.service.imp;

import com.huangpuweb.api.model.*;
import com.huangpuweb.api.service.ApiService;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.smartcardio.ATR;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

@Service
public class ApiServiceImp extends BaseSysDataServiceImp implements ApiService {


    @Override
    public Integer getCityRubbishExposedDayCount() {
        String sql = "select count(1) as dayCount from sys_lj_eventdata where TO_DAYS(create_time) = TO_DAYS(NOW()) ";
        return queryInteger(sql, new Object[]{});
    }

    @Override
    public Integer getCityRubbishExposedMonthCount() {
        String sql = "SELECT count(1) as monthCount FROM sys_lj_eventdata  WHERE  DATE_FORMAT( create_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )";
        return queryInteger(sql, new Object[]{});
    }

    @Override
    public Integer getCityRubbishExposedSum() {
        String sql = "select count(distinct (date(create_time))) as sum from sys_lj_eventdata";
        return queryInteger(sql, new Object[]{});
    }

    @Override
    public List<DiscoverDisposal> getCityRubbishExposedDiscoverDisposalCount(String year) {
        String sql = "SELECT month (create_time) as month ,count(create_time) as count from sys_lj_eventdata WHERE year(create_time)=? GROUP BY month(create_time);";
        return getEntryList(sql, new DiscoverDisposalMapper(), new Object[]{year});
    }

    @Override
    public List<YearSpotWarn> getEveryYearSpotWarnCount(String year) {
     String sql="select * from app_ssrlsj_yjz_count where year=?";
        return getEntryList(sql, new YearSpotWarnMapper(), new Object[]{year});
    }


    @Override
    public void insert(String valueOf, String valueOf1, String deviceid) {
        String sql = "update  batch_stream_status set lng=?,lat=? where DeviceID=? ";
        update(sql, new Object[]{valueOf, valueOf1, deviceid});
    }

    @Override
    public List<BatchStreamStatus> getList() {
        String sql = "select * from batch_stream_status";
        return getEntryList(sql, new RowMapper<BatchStreamStatus>() {
            @Override
            public BatchStreamStatus mapRow(ResultSet rs, int rowNum) throws SQLException {
                BatchStreamStatus bs=new BatchStreamStatus();
                bs.setDeviceid(rs.getString("DeviceID"));
                bs.setDevicename(rs.getString("DeviceName"));
                bs.setVideoScenic(rs.getString("VideoScenic"));
                bs.setLng(rs.getString("lng"));
                bs.setLat(rs.getString("lat"));
                return bs;
            }
        },new Object[]{});
    }

    @Override
    public List<BatchStreamStatus> getAllVideoList(String type) {
        String sql;
        if ("waitan".equals(type)) {
           sql ="select * from batch_stream_status where VideoScenic='外滩'";
        }else {
            sql ="select * from batch_stream_status where VideoScenic<>'外滩'";
        }

        return getEntryList(sql, new RowMapper<BatchStreamStatus>() {
            @Override
            public BatchStreamStatus mapRow(ResultSet rs, int rowNum) throws SQLException {
                BatchStreamStatus bs=new BatchStreamStatus();
                bs.setDeviceid(rs.getString("DeviceID"));
                bs.setDevicename(rs.getString("DeviceName"));
                bs.setVideoScenic(rs.getString("VideoScenic"));
                bs.setLng(rs.getString("lng"));
                bs.setLat(rs.getString("lat"));
                return bs;
            }
        },new Object[]{});
    }

    @Override
    public List<SysHpRoadPreCondition> getRoadPredData(String roadName) {
        String sql="SELECT p.predTime,p.speed from hp_road_prediction p  join hp_road r on p.roadId=r.id WHERE r.rid_name=?";
        return getEntryList(sql,new SysHpRoadPreConditionMapper(),new Object[]{roadName});
    }
}
